﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthCenter.Tools.Password
{
    public static class PassGen
    {
        private static readonly Random random = new Random();

        public static string MakePassword(int length)
        {
            const string specialChars = "!@#$%^&*()_+{}[]|\":;'<>?,./";
            const string uppercaseChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            const string lowercaseChars = "abcdefghijklmnopqrstuvwxyz";
            const string digits = "0123456789";

            char[] password = new char[length];
            int specialCharIndex, uppercaseIndex, lowercaseIndex, digitIndex;

            for (int i = 0; i < length; i++)
            {
                // 随机选择一个特殊字符、大写字母、小写字母或数字
                int charType = random.Next(4);
                switch (charType)
                {
                    case 0: // 特殊字符
                        specialCharIndex = random.Next(specialChars.Length);
                        password[i] = specialChars[specialCharIndex];
                        break;
                    case 1: // 大写字母
                        uppercaseIndex = random.Next(uppercaseChars.Length);
                        password[i] = uppercaseChars[uppercaseIndex];
                        break;
                    case 2: // 小写字母
                        lowercaseIndex = random.Next(lowercaseChars.Length);
                        password[i] = lowercaseChars[lowercaseIndex];
                        break;
                    case 3: // 数字
                        digitIndex = random.Next(digits.Length);
                        password[i] = digits[digitIndex];
                        break;
                }
            }

            return new string(password);
        }
    }
}
